package org.itboys.mobile.controller.web;

import org.itboys.commons.CommonConstants;
import org.itboys.commons.dto.ResultPageDOHelper;
import org.itboys.commons.utils.ajax.AjaxUtils;
import org.itboys.framework.query.JsonPageUtils;
import org.itboys.framework.spring.controller.BaseController;
import org.itboys.mobile.entity.mongo.SundayFloorMr;
import org.itboys.mobile.entity.mongo.SundayFloorMrGroup;
import org.itboys.mobile.service.mongo.web.SundayFloorMrGroupService;
import org.itboys.mobile.util.ResponseMessage;
import org.itboys.param.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Author：贾琦磊
 * @Date：2018/7/18
 * @Description：后台管理楼先生分组
 */
@Controller
@RequestMapping("/sunday/web/floorMr/group")
public class SundayFloorMrGroupController extends BaseController{
    @Autowired
    private SundayFloorMrGroupService groupService;
    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response){
        return "sunday/floorMr/group/index";
    }
    @RequestMapping("/input")
    public String input(HttpServletRequest request, HttpServletResponse response,
                        @RequestParam("id")Long id,
                        Model model){
        try {
            model.addAttribute("floorMrGroup",groupService.getById(id));
            return "sunday/floorMr/group/input";
        } catch (Exception e) {
            String msg = "";
            if (e.getClass().getSimpleName().equalsIgnoreCase("ErrorException")) {
                msg = e.getMessage();
            } else {
                msg = CommonConstants.ERROMSG;
                logger.error("/sunday/web/floorMr/input------" + e.getMessage());
            }
            e.printStackTrace();

            ResponseMessage.error(CommonConstants.FAIL_CODE,msg);
        }
        return "sunday/activity/error";
    }
    @RequestMapping("/select")
    public void select(HttpServletRequest request, HttpServletResponse response){
        try {
            PageResult<SundayFloorMrGroup> result = groupService.select(request);
            JsonPageUtils.renderJsonPage(result.getTotal(),result.getData(),response);
        } catch (Exception e) {
            e.printStackTrace();
            AjaxUtils.renderJsonporJson(response, null,
                    ResultPageDOHelper.getMsgCodeResultDO(CommonConstants.FAIL_CODE, CommonConstants.FAIL));
        }
    }
    @RequestMapping("/save")
    public void save(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id")Long id
            ,@RequestParam("title")String title
           ,@RequestParam("subTitle")String subTitle
            ,@RequestParam("link")String link
            //,@RequestParam("desc")String desc
            ,@RequestParam(value = "image",required = false)MultipartFile image){

        try {
            groupService.save( id,title,subTitle,link,image);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }

    @RequestMapping("/delete")
    public void delete(HttpServletRequest request,HttpServletResponse response
            ,@RequestParam("id")Long id){
        try {
            groupService.delete(id);
            ResponseMessage.success();
        } catch (Exception e) {
            e.printStackTrace();
            ResponseMessage.error(CommonConstants.FAIL_CODE,"error");
        }
    }

    /**
     * 查询字典
     */
    @RequestMapping("/getDictionaryByKey")
    public void getDictionaryByKey(  @RequestParam(value="key",required=false) String key, HttpServletResponse response) {
        AjaxUtils.renderJson(response, groupService.getDictionary());
    }
}
